<?php $this->layoutNoTitle = true ?>
<h1><?php __e('Conversation') ?></h1>
<div class="helpdesk">
    <?php if ($strategy instanceof Am_Helpdesk_Strategy_Admin) : ?>
    <div class="helpdesk-history">
        <div class="helpdesk-history-title">
            <div class="helpdesk-history-title-action"><a href="javascript:;">Requester History</a></div>
            <div class="helpdesk-history-user">
                <strong><?php p($user->login) ?> (<?php p($user->getName()) ?>)</strong>
            </div>
        </div>
        <div class="helpdesk-history-content">
            <?php echo $historyGrid ?>
        </div>
    </div>
    <?php endif; ?>
    <div class="ticket" id="helpdesk-ticket-<?php echo $ticket->ticket_id ?>">
        <div class="status">
            <?php
            if ($strategy instanceof Am_Helpdesk_Strategy_Admin) {
                include $this->_script($strategy->getTemplatePath() . '/_select-status-form.phtml');
            }
            ?>
        </div>
        <h3><?php p($ticket->subject) ?> <span class="ticketMask">#<?php p($ticket->ticket_mask) ?></span></h3>
        <div class="clear"></div>
        <div class="actions-conteiner">
        <div class="actions">
            <?php if ($strategy instanceof Am_Helpdesk_Strategy_Admin) : ?>
            <div><a href="<?php
            echo Am_Di::getInstance()->helpdeskStrategy->assembleUrl(array(
                'page_id' => 'view',
                'action' => 'reply',
                'ticket_id' => $ticket->ticket_id,
                'type' => 'comment'
            ), 'inside-pages')
            ?>" class="a-reply"><?php __e('Comment') ?></a></div>
            <?php endif; ?>
            <div><a href="<?php
            echo Am_Di::getInstance()->helpdeskStrategy->assembleUrl(array(
                'page_id' => 'view',
                'action' => 'reply',
                'ticket_id' => $ticket->ticket_id
            ), 'inside-pages')
            ?>" class="a-reply"><?php __e('Reply') ?></a></div>
            <?php if ($ticket->status == HelpdeskTicket::STATUS_CLOSED) : ?>
                <div><a href="<?php
            echo Am_Di::getInstance()->helpdeskStrategy->assembleUrl(array(
                'page_id' => 'view',
                'action' => 'changestatus',
                'ticket' => $ticket->ticket_mask,
                'status' => HelpdeskTicket::STATUS_NEW
            ), 'inside-pages')
            ?>"><?php __e('Re-Open Ticket') ?></a></div>
            <?php else : ?>
                <div><a href="<?php
            echo Am_Di::getInstance()->helpdeskStrategy->assembleUrl(array(
                'page_id' => 'view',
                'action' => 'changestatus',
                'ticket' => $ticket->ticket_mask,
                'status' => HelpdeskTicket::STATUS_CLOSED
            ), 'inside-pages')
            ?>"><?php __e('Close Ticket') ?></a></div>
            <?php endif; ?>
            <div><a href="<?php
            echo Am_Di::getInstance()->helpdeskStrategy->assembleUrl(array(
                'page_id' => 'view',
                'action' => 'view',
                'ticket' => $ticket->ticket_mask,
            ), 'inside-pages')
            ?>"><?php __e('Refresh') ?></a></div>
            <div class="right">[<a href="javascript:;" class="closeAll">&minus;</a>]</div>
        </div>
        <div class="reply"></div>
        </div>
    </div>
    <?php 
        foreach ($ticket->getMessages() as $message) {
            include $this->_script($strategy->getTemplatePath() . '/_message.phtml');
        }
    ?>
</div>
<script>
(function($){
    $(function() {
        $(".helpdesk .helpdesk-history-title-action a").click(function(){
            $('.helpdesk-history-content').slideToggle();
        })
        $(".helpdesk a.a-reply").click(function(){
            $(".helpdesk a.a-reply").parent().removeClass('active');
            $(this).parent().addClass('active');

            var reply = $(this).parents('.actions').siblings('.reply');

            var buffer = '';
            if (reply.html()!='') {
                buffer = reply.find('textarea').attr('value');
            }

            $('.helpdesk .reply').empty().hide();
            reply.load($(this).attr('href'), function(){
                if (buffer) {
                    $(this).find('textarea').attr('value', buffer);
                }
            })
            .append('<img src="<?php echo REL_ROOT_URL ?>/application/default/views/public/img/ajax-loader.gif" />').show();

            return false;
        })

        $(".helpdesk .status input[type=submit]").hide();
        $(".helpdesk .status select").change(function(){
            this.form.submit();
        });

        $('.helpdesk input[name=discard]').live('click', function(){
           $(".helpdesk a.a-reply").parent().removeClass('active');
           $(this).parents('.reply').empty().hide();
        })

        $(".helpdesk .close").click(function(){
            var $message = $(this).closest('.header').next('.content');
            var $messageConteiner = $(this).closest('.message');
            if ($message.css('display')=='none') {
                $(this).html('&minus;');
                $message.show('slow');
                $messageConteiner.removeClass('closed');
            } else {
                $(this).html('+');
                $message.hide('slow');
                $messageConteiner.addClass('closed');
            }
        })

        $(".helpdesk .closeAll").click(function(){
            var $messages = $('.helpdesk .content');
            var $messageConteiners = $('.helpdesk .message');
            if ($(this).hasClass('closed')) {
                $(this).removeClass('closed');
                $('.helpdesk .close').html('&minus;');
                $(this).html('&minus;');
                $messages.show('slow');
                $messageConteiners.removeClass('closed');
            } else {
                $(this).addClass('closed');
                $('.helpdesk .close').html('+');
                $(this).html('+');
                $messages.hide('slow');
                $messageConteiners.addClass('closed');
            }
        })
    });
})(jQuery)
</script>